package com.lh.glorypavilion.service.edu.controller.admin;


import com.lh.glorypavilion.exception.glorypavilionEx;
import com.lh.glorypavilion.result.R;
import com.lh.glorypavilion.result.ResultCode;
import com.lh.glorypavilion.service.edu.entity.vo.SubjectVo;
import com.lh.glorypavilion.service.edu.service.ISubjectService;
import com.lh.glorypavilion.util.ExceptionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author lhui
 * @since 2020-10-12
 */
//@CrossOrigin
@Api(description = "课程分类管理")
@RestController
@RequestMapping("/admin/edu/subject")
@Slf4j
public class SubjectController {

    @Autowired
    private ISubjectService iSubjectService;

    @ApiOperation("批量导入Excel数据")
    @PostMapping("import")
    public R batchImport(@ApiParam(value = "Excel文件",required = true)
                             @RequestParam("file") MultipartFile file){
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            iSubjectService.batchImport(inputStream);
            return R.ok().message("数据导入成功！");
        } catch (IOException e) {
            log.info(ExceptionUtils.getMessage(e));
            throw new glorypavilionEx(ResultCode.EXCEL_DATA_IMPORT_ERROR);
        }
    }

    @ApiOperation("嵌套数据列表")
    @GetMapping("nested-list")
    public R nestedList(){
        List<SubjectVo> SubjectVoList=iSubjectService.nestedList();
        return R.ok().data("items",SubjectVoList);
    }

}

